var rp = require('request-promise');
const cheerio = require('cheerio')
const mongoose = require('mongoose');
mongoose.connect('mongodb://itcast:itcast@localhost:27017/alibaixiu', {useNewUrlParser: true,useUnifiedTopology: true})
    .then(() => console.log('数据库连接成功'))
    .catch(() => console.log('数据库连接失败'));
const { Schema } = mongoose;
// 数据集合规则
const BaiduSchema = new Schema({
    // 标题
    title: {
        type: String
    },
    // 摘要
    abstract: {
        type: String
    },
    // 链接
    link: {
        type: String
    }
});
//数据类
const Baidu = mongoose.model('Baidu', BaiduSchema);
const fs = require('fs')
rp('http://www.baidu.com/s?wd=JavaScript&rsv_spt=1&rsv_iqid=0xeab05c74001088e2&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ib&rsv_sug3=16&rsv_sug1=10&rsv_sug7=100&rsv_sug2=0&inputT=2950&rsv_sug4=2950')
    .then(function (htmlString) {
        const $ = cheerio.load(htmlString)
        const $data = $('.c-container').slice(0,5)//只保留前面5条数据
        $data.each(async (i,item)=>{
            var title = $(item).find('h3').text()
            var abstract = $(item).find('.c-abstract').text()
            var link = $(item).find('h3 a').attr('href')
            await Baidu.create({
                title,
                abstract,
                link 
            })
        })
    })
    .catch(function (err) {
        // Crawling failed...
    });